#!/usr/bin/sh
##########################################################################
#
# Filename :	ftpscript.sh
#
# Description :	Script to run FTP program to get and put file
#		(Compulsory Input Parameter)
#		$1  - FTP Host Name
#		$2  - FTP User Name
#		$3  - FTP User Password
#		$4  - Local Directory
#		$5  - Source Filename
#		$6  - Remote Directory
#		(Optional Parameters)
#		$7  - Operation (get,mget,[put],mput)
#		$8  - Destination Filename
#
# Version :	1.1
#
# History :	21 Jan, 2002 v1.0	Created by Billy Kwok
#					put file to remote server
#		23 Aug, 2002 v1.1	get file from remote server added
#		03 Oct, 2002		mget and mput functions added
#		06 Dec, 2002		check for the existence of the 
#					destfile and return correct return
#					code
#
##########################################################################
PATH=$PATH:/usr/bin/ftp
#
# check for enough input parameters
#
if test $# -lt 6; then
	printf "FTPScript 1.1 of 03 October 2002, by Billy Kwok\n\n"
	echo "Usage: $0 <host> <ftp_user> <ftp_passwd> <local_path> <source_file_name> <remote_path> [<get|mget|put|mput>] [<dest_file_name>]"
	exit 1
fi
#
if test $8
then
	dest_filename="$8"
else
	dest_filename="$5"
fi
#
#
cd $4
if [ "$7" = "get" -o "$7" = "GET" ]; then
	msg=`ftp -n $1<<EOF
	user $2 $3
	cd $6
	get $5 $dest_filename
	bye
	EOF`
elif [ "$7" = "mget" -o "$7" = "MGET" ]; then
	msg=`ftp -n $1<<EOF
	user $2 $3
	cd $6
	prompt
	mget $dest_filename
	bye
	EOF`
elif [ "$7" = "mput" -o "$7" = "MPUT" ]; then
	msg=`ftp -n $1<<EOF
	user $2 $3
	cd $6
	prompt
	mput $dest_filename
	bye
	EOF`
else
	msg=`ftp -n $1<<EOF
	user $2 $3
	cd $6
	put $5 $dest_filename
	bye
	EOF`
fi
# Check for the existence of the dest_file
ls $dest_filename > /dev/null
exit $?
#
#
